home *** CD-ROM | disk | FTP | other *** search
/ 220 Jogos / 220 jogos.iso / classicos / resta11 / MainWindow.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-24  |  10.9 KB  |  559 lines

  1. // MainWindow.cpp: Implementierungsdatei
  2. //
  3. // Copyright by AndrΘ Stein
  4. // E-Mail: stonemaster@steinsoft.net, andre_stein@web.de
  5. // http://www.steinsoft.net
  6. //////////////////////////////////////////////////////////////////////
  7.  
  8. #include "stdafx.h"
  9. #include "OpenGLMfc.h"
  10. #include "MainWindow.h"
  11.  
  12. #include <stdlib.h>
  13. #include <time.h>
  14.  
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20.  
  21.  
  22. #define ROT_SPEED 0.03f
  23. #define ZOOM_SPEED 0.001f
  24.  
  25. //** LICHT **
  26. const float WHOLE_SCENE_LIGHT[] = { 0.25f, 0.25f, 0.25f, 1.0f};
  27. const float DIFFUSE_LIGHT[]  = { 0.6f, 0.6f, 0.6f, 1.0f};
  28. const float SPECULAR_LIGHT[] = {-.1f, -0.1f, -0.1f, 1.0f};
  29. const float LIGHT_POSITION1[] = { 0.0f, -5.0f,0.0f, 1.0f};
  30. const float LIGHT_POSITION2[] = { 0.0f, 5.0f,0.0f, 1.0f};
  31.  
  32. const float SPOT_DIRECTION[] = {0.0f,0.0f,-1.0f}; 
  33. const float MATERIAL[] = {1.0f,1.0f,1.0f,1.0f};
  34.  
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CMainWindow
  37.  
  38. CMainWindow::CMainWindow()
  39. {
  40.     clientDC = NULL;
  41.     fullscreen = true;
  42.     mousePosition[0] = 0.0f;
  43.     mousePosition[1] = 0.0f;
  44.     mouseDown = false;
  45.     movement[0] = -8.0f;
  46.     movement[1] = 0.0f;
  47.  
  48.     demoState = STATE_INTRO;
  49.  
  50.     //rand() initialisieren
  51.     srand(time(NULL));
  52.  
  53. CMainWindow::~CMainWindow()
  54. {
  55.     if (clientDC)
  56.     {
  57.         delete clientDC;
  58.     }
  59. }
  60.  
  61.  
  62. BEGIN_MESSAGE_MAP(CMainWindow, CWnd)
  63.     //{{AFX_MSG_MAP(CMainWindow)
  64.     ON_WM_SIZE()
  65.     ON_WM_CLOSE()
  66.     ON_WM_SYSCOMMAND()
  67.     ON_WM_CREATE()
  68.     ON_WM_KEYDOWN()
  69.     ON_WM_SETFOCUS()
  70.     ON_WM_KILLFOCUS()
  71.     ON_WM_LBUTTONDOWN()
  72.     ON_WM_LBUTTONUP()
  73.     //}}AFX_MSG_MAP
  74. END_MESSAGE_MAP()
  75.  
  76.  
  77. /////////////////////////////////////////////////////////////////////////////
  78. // Behandlungsroutinen fⁿr Nachrichten CMainWindow 
  79.  
  80. void CMainWindow::OnSize(UINT nType, int cx, int cy) 
  81. {
  82.     CWnd::OnSize(nType, cx, cy);
  83.     
  84.     if (cy == 0)                                
  85.     {
  86.         cy = 1;                        
  87.     }
  88.  
  89.     glViewport(0, 0, cx, cy);    
  90.  
  91.     glMatrixMode(GL_PROJECTION);                        
  92.     glLoadIdentity();                        
  93.  
  94.     
  95.     gluPerspective(45.0f,(GLfloat)cx/(GLfloat)cy,0.1f,100.0f);
  96.  
  97.     glMatrixMode(GL_MODELVIEW);                        
  98.     glLoadIdentity();
  99. }
  100.  
  101. void CMainWindow::Update()
  102. {
  103.     //***MOUSE***
  104.  
  105.     static CPoint lastMouse;
  106.  
  107.     GetCursorPos(&lastMouse);
  108.     SetCursorPos(320,240);
  109.         
  110.     float moveX = float((320 - lastMouse.x))/100.0f;
  111.     float moveY = float((240 - lastMouse.y))/100.0f;
  112.  
  113.     if (!(mousePosition[0] >= 5.0f || mousePosition[0] <= -5.0f ||
  114.         mousePosition[1] >= 4.0f || mousePosition[1] <= -4.0f))
  115.     {
  116.         //Die Mausbewegung aktualisieren
  117.         mousePosition[0] -= moveX;
  118.         mousePosition[1] += moveY;
  119.     }
  120.     else
  121.     {
  122.         mousePosition[0] = -mousePosition[0] -
  123.             (mousePosition[0] < 0.0f ? 0.05f : -0.05f);
  124.         mousePosition[1] = -mousePosition[1] -
  125.             (mousePosition[1] < 0 ? 0.05f:-0.05f);
  126.     }
  127.  
  128.  
  129.  
  130.     //Frametime ausrechnen
  131.     static float framestart = timer.getTime();
  132.     static float frameend = 0.0f;
  133.     
  134.     do 
  135.     {
  136.         frameend = timer.getTime();
  137.     }
  138.     while (frameend == framestart);
  139.  
  140.     frametime  = frameend - framestart;
  141.     framestart = frameend;
  142.  
  143.     if (GetAsyncKeyState(VK_ESCAPE))
  144.     {
  145.         if (demoState == STATE_GAME || demoState == STATE_END ||
  146.             demoState == STATE_MOVING)
  147.         {
  148.             PostMessage(WM_CLOSE);
  149.         }
  150.         else if (demoState == STATE_INTRO)
  151.         {
  152.             demoState = STATE_GAME;
  153.             /**/
  154.             Sleep(500);
  155.         }
  156.         
  157.     }
  158.  
  159.     //***************
  160.     // Treefield updaten
  161.     if (demoState == STATE_GAME)
  162.     {
  163.         if (mouseDown)
  164.         {
  165.             treefield.move(mousePosition[0],mousePosition[1]);
  166.         }
  167.  
  168.         if (treefield.isDone())
  169.         {
  170.             demoState = STATE_MOVING;
  171.         }
  172.     }
  173.     else if (demoState == STATE_INTRO)
  174.     {
  175.         if (intro.isDone())
  176.         {
  177.             //FadeOut(750);
  178.             demoState = STATE_GAME;
  179.         }
  180.     }
  181.     else if (demoState == STATE_MOVING)
  182.     {
  183.         movement[0] += ZOOM_SPEED*frametime;
  184.         movement[1] += ROT_SPEED*frametime;
  185.  
  186.         if (movement[0] >= 2.0f)
  187.         {
  188.             demoState = STATE_END;
  189.             endScene.create(clientDC->m_hDC,treefield.treesLeft());
  190.             soundSystem.stopMusic(0);
  191.             soundSystem.playMusic(STREAM_BELLS);
  192.         }
  193.     }
  194.     else
  195.     {
  196.         if (endScene.isDone())
  197.         {
  198.             PostQuitMessage(0);
  199.         }
  200.     }
  201.     
  202.  
  203.     DrawGLScene();
  204. }
  205.  
  206. void CMainWindow::DrawGLScene()
  207. {
  208.     glClear(GL_COLOR_BUFFER_BIT |
  209.             GL_DEPTH_BUFFER_BIT);
  210.  
  211.     glLoadIdentity();
  212.  
  213.     if (demoState == STATE_GAME)
  214.     {
  215.         ////////////////////
  216.         // Drawing code here
  217.         ////////////////////
  218.  
  219.         glTranslatef(0.0f,0.0f,-8.0f);
  220.  
  221.         treefield.update(frametime);
  222.         snowflakes.update(frametime);
  223.  
  224.         DrawMouseCursor();
  225.     
  226.     }
  227.     else if (demoState == STATE_INTRO)
  228.     {
  229.         //TRACE0("ZEICHE INTRO...\n");
  230.         intro.update(frametime);
  231.     }
  232.     else if (demoState == STATE_MOVING)
  233.     {
  234.         snowflakes.update(frametime);
  235.         
  236.         glTranslatef(0.0f,0.0f,movement[0]);
  237.         glRotatef(movement[1],0.0f,0.0f,1.0f);
  238.  
  239.         treefield.update(frametime);
  240.         
  241.     }
  242.     else
  243.     {
  244.         endScene.update(frametime);
  245.     }
  246.  
  247.     SwapBuffers(clientDC->m_hDC);
  248. }
  249.  
  250. void CMainWindow::OnClose() 
  251. {
  252.     screenMode.reset();
  253.     
  254.     PostQuitMessage(0);
  255.  
  256.     ShowCursor(true);
  257.  
  258.     CWnd::OnClose();
  259. }
  260.  
  261. void CMainWindow::InitGL()
  262. {
  263.     glShadeModel(GL_SMOOTH);
  264.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  265.     glClearDepth(1.0f);                            
  266.     glEnable(GL_DEPTH_TEST);                    
  267.     glDepthFunc(GL_LEQUAL);    
  268.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  269.  
  270.     glBlendFunc(GL_ONE,GL_ONE);
  271.  
  272.     glEnable(GL_TEXTURE_2D);
  273.  
  274.     /************************
  275.             LIGHTING
  276.     *************************/
  277.  
  278.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT,WHOLE_SCENE_LIGHT);
  279.  
  280.     //Main
  281.     glLightfv(GL_LIGHT0,GL_AMBIENT,SPECULAR_LIGHT);
  282.     glLightfv(GL_LIGHT0,GL_SPECULAR,SPECULAR_LIGHT);
  283.     glLightfv(GL_LIGHT0,GL_DIFFUSE,DIFFUSE_LIGHT);
  284.     glLightfv(GL_LIGHT0,GL_POSITION,LIGHT_POSITION1);
  285.  
  286.     glLightfv(GL_LIGHT1,GL_AMBIENT,SPECULAR_LIGHT);
  287.     glLightfv(GL_LIGHT1,GL_SPECULAR,SPECULAR_LIGHT);
  288.     glLightfv(GL_LIGHT1,GL_DIFFUSE,DIFFUSE_LIGHT);
  289.     glLightfv(GL_LIGHT1,GL_POSITION,LIGHT_POSITION2);
  290.  
  291.     //Spotlight
  292.     glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 10.0f); //60░ Spotlight
  293.     glLightf(GL_LIGHT0,GL_SPOT_EXPONENT, 60.0f);
  294.     glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,SPOT_DIRECTION);
  295.  
  296.  
  297.     glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
  298.  
  299.     //Materials
  300.     glMaterialfv(GL_FRONT,GL_SPECULAR,MATERIAL);
  301.     glMaterialf(GL_FRONT,GL_SHININESS,32.0f);
  302.  
  303.     glEnable(GL_COLOR_MATERIAL);
  304.     glEnable(GL_LIGHTING);
  305.     glEnable(GL_LIGHT0);
  306.     glEnable(GL_LIGHT1);
  307.  
  308.  
  309.     ///////////////////////////
  310.     // Textures
  311.     ///////////////////////////
  312.  
  313.     textures[TEX_BAUM].createMipmap("Data/BaumOben.bmp");
  314.     textures[TEX_BAUM_STAMM].createMipmap("Data/BaumStamm.bmp");
  315.     textures[TEX_SNOWFLAKE].createMipmap("Data/Snowflake.bmp");
  316.     textures[TEX_FLASCHE].createMipmap("Data/Flasche.bmp");
  317.  
  318.     treefield.create(&textures[TEX_BAUM],&textures[TEX_BAUM_STAMM],&soundSystem);
  319.     snowflakes.create(&textures[TEX_SNOWFLAKE]);
  320.     intro.create(clientDC->GetSafeHdc());
  321.     soundSystem.init(m_hWnd);
  322.  
  323.     soundSystem.playMusic(STREAM_RUDI);
  324.  
  325.     /////////////////////////////////////////////////////////////////
  326.  
  327.     timer.create();
  328. }
  329.  
  330. void CMainWindow::OnSysCommand(UINT nID, LPARAM lParam) 
  331. {
  332.     if (nID == SC_SCREENSAVE || 
  333.         nID == SC_MONITORPOWER)
  334.     {
  335.         return;
  336.     }
  337.  
  338.     CWnd::OnSysCommand(nID, lParam);
  339. }
  340.  
  341. void CMainWindow::CreateGLWindow()
  342. {
  343.     if (fullscreen)
  344.     {
  345.         screenMode.setDisplayMode(640,480,16);
  346.     }
  347.         
  348.     
  349.     //Windowcreation
  350.     CString className = AfxRegisterWndClass(
  351.         CS_HREDRAW | CS_VREDRAW | CS_OWNDC,
  352.         NULL,
  353.         (HBRUSH)GetStockObject(BLACK_BRUSH),
  354.         AfxGetApp()->LoadIcon(IDR_MAINFRAME));
  355.  
  356.     CreateEx(
  357.         0,
  358.         className,
  359.         "OpenGL",
  360.         (fullscreen ? WS_POPUP : WS_POPUP|WS_CAPTION),
  361.         (fullscreen ? CRect(0,0,640,480) : CRect(50,50,640,480)),
  362.         NULL,
  363.         0);
  364.  
  365.     //Show Window and make active
  366.     ShowWindow(SW_SHOW);
  367.     SetForegroundWindow();
  368.     SetFocus();
  369.         
  370.     
  371.     //No cursor visible
  372.     ShowCursor(false);
  373. }
  374.  
  375. int CMainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  376. {
  377.     if (CWnd::OnCreate(lpCreateStruct) == -1)
  378.         return -1;
  379.     
  380.     //OpenGL init
  381.     
  382.     clientDC = new CClientDC(this);
  383.     openGLDevice.create(clientDC->m_hDC);
  384.     InitGL();
  385.     
  386.     return 0;
  387. }
  388.  
  389. void CMainWindow::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  390. {
  391.     switch (nChar)
  392.     {
  393.     case VK_F9:
  394.         fullscreen = !fullscreen;
  395.         UpdateView();
  396.         break;
  397.     case VK_F7:
  398.         Minimize();
  399.         break;
  400.     }
  401.     
  402.     CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
  403. }
  404.  
  405. void CMainWindow::UpdateView()
  406. {
  407.     if (fullscreen)
  408.     {
  409.         screenMode.setDisplayMode(640,480,16);
  410.         
  411.         ModifyStyle(WS_CAPTION,0);
  412.         MoveWindow(CRect(0,0,640,480));
  413.     }
  414.     else
  415.     {    
  416.         screenMode.reset();
  417.         
  418.         ModifyStyle(0,WS_CAPTION);
  419.         MoveWindow(CRect(50,50,640,480));
  420.     }
  421.  
  422.         
  423. }
  424.  
  425. void CMainWindow::OnSetFocus(CWnd* pOldWnd) 
  426. {
  427.     CWnd::OnSetFocus(pOldWnd);
  428.     
  429.     UpdateView();
  430. }
  431.  
  432. void CMainWindow::OnKillFocus(CWnd* pNewWnd) 
  433. {
  434.     CWnd::OnKillFocus(pNewWnd);
  435.     
  436.     Minimize();
  437.     
  438. }
  439.  
  440. void CMainWindow::Minimize()
  441. {
  442.     if (IsIconic())
  443.     {
  444.         return;
  445.     }
  446.         
  447.     WINDOWPLACEMENT placement;
  448.     GetWindowPlacement(&placement);
  449.  
  450.     if (fullscreen)
  451.     {
  452.         screenMode.reset();
  453.     }
  454.  
  455.     placement.showCmd = SW_SHOWMINIMIZED;
  456.     
  457.     SetWindowPlacement(&placement);
  458. }
  459.  
  460.  
  461. void CMainWindow::DrawMouseCursor()
  462. {
  463.     glPushMatrix();
  464.  
  465.     glEnable(GL_BLEND);
  466.     glDisable(GL_DEPTH_TEST);
  467.  
  468.     textures[TEX_FLASCHE].select();
  469.     
  470.     glBegin(GL_QUADS);
  471.         
  472.         glTexCoord2f(0.0f,1.0f); glVertex3f(mousePosition[0],mousePosition[1],
  473.             (mouseDown ? 0.75f: 0.0f));
  474.         glTexCoord2f(0.0f,0.0f); glVertex3f(mousePosition[0],mousePosition[1]-1.0f,
  475.             (mouseDown ? 0.75f: 0.0f));
  476.         glTexCoord2f(1.0f,0.0f); glVertex3f(mousePosition[0]+0.75f,mousePosition[1]-1.0f,
  477.             (mouseDown ? 0.75f: 0.0f));
  478.         glTexCoord2f(1.0f,1.0f); glVertex3f(mousePosition[0]+0.75f,mousePosition[1],
  479.             (mouseDown ? 0.75f: 0.0f));
  480.     glEnd();
  481.  
  482.     glDisable(GL_BLEND);
  483.     glEnable(GL_DEPTH_TEST);
  484.  
  485.     glPopMatrix();
  486. }
  487.  
  488. void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point) 
  489. {
  490.     if (demoState == STATE_GAME)
  491.     {
  492.         if (treefield.click(mousePosition[0],mousePosition[1]))
  493.         {
  494.             mouseDown = true;
  495.         }
  496.     }
  497.         
  498.     CWnd::OnLButtonDown(nFlags, point);
  499. }
  500.  
  501. void CMainWindow::OnLButtonUp(UINT nFlags, CPoint point) 
  502. {
  503.     if (demoState == STATE_GAME)
  504.     {
  505.         mouseDown = false;
  506.         treefield.release(mousePosition[0],mousePosition[1]);
  507.     }
  508.     
  509.     CWnd::OnLButtonUp(nFlags, point);
  510. }
  511.  
  512. void CMainWindow::FadeOut(int delay)
  513. {
  514.     STimer timer;
  515.     timer.create();
  516.  
  517.     float time = 0.0f;
  518.     float alpha = 0.0f;
  519.     
  520.     
  521.     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  522.     glEnable(GL_BLEND);
  523.     glDisable(GL_DEPTH_TEST);
  524.  
  525.     for (;;)
  526.     {
  527.         //Zeit festhalten und ms seit Anfang dazurechnen
  528.         time += timer.getTime();
  529.         
  530.         //Normales Qudrad zeichnen dass
  531.         //je nach Fadelevel durchsichtig ist
  532.         //(Position = try-and-error ;))
  533.         glBegin(GL_QUADS);
  534.             glColor4f(0.0f,0.0f,0.0f,alpha);
  535.             glVertex3f(1.15f,1.15f,-2.0f);
  536.             glVertex3f(-1.15f,1.15f,-2.0f);
  537.             glVertex3f(-1.15f,-1.15f,-2.0f);
  538.             glVertex3f(1.15f,-1.15f,-2.0f);
  539.         glEnd();
  540.  
  541.         glFlush();
  542.         SwapBuffers(clientDC->m_hDC);
  543.  
  544.         //Alpha wird berrechnet nach Relation zwischen
  545.         //abgelaufener Zeit(time) und dem Limit(delay) 
  546.         alpha = (time/delay)/1000;
  547.  
  548.         if (alpha >= 1.0f)
  549.         {
  550.             break;
  551.         }
  552.     }
  553.  
  554.     glDisable(GL_BLEND);
  555.     glEnable(GL_DEPTH_TEST);
  556.  
  557.     glBlendFunc(GL_ONE,GL_ONE);
  558. }